package com.sd365.permission.centre.api.controller;

import com.sd365.common.core.common.api.CommonPage;
import com.sd365.common.log.api.annotation.ApiLog;
import com.sd365.permission.centre.api.RoleApi;
import com.sd365.permission.centre.entity.Node;
import com.sd365.permission.centre.pojo.dto.*;
import com.sd365.permission.centre.pojo.query.RoleQuery;
import com.sd365.permission.centre.pojo.query.UserQuery;
import com.sd365.permission.centre.pojo.vo.RoleCompanyVO;
import com.sd365.permission.centre.pojo.vo.RoleVO;
import com.sd365.permission.centre.service.RoleService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @version 1.0
 * @ClassName: RoleController
 * @Description: 角色管理的接口实现，包括增删改查和用户资源关联的功能
 * @Author: Hejiale
 * @Date: 2023-08-06 10:11
 **/
@CrossOrigin
@Validated
@RestController
public class RoleController implements RoleApi {

    @Autowired
    private RoleService roleService;

    @Override
    public Boolean initRoleResourceRedis(){
        return roleService.loadRoleResource2Cache();
    }

    @ApiLog
    @Override
    public Boolean assignResource(@NotNull @Valid
                                      @RequestBody RoleResourceDTO roleResourceDTO) {
        return roleService.assignResource(roleResourceDTO);
    }

    @ApiLog
    @Override
    public Boolean assignUser(@NotNull @Valid
                                  @RequestBody UserRoleDTO userRoleDTO) {
        return roleService.assignUser(userRoleDTO);
    }
/**
 * 弃用，不传id
 * 故设置为系统管理员id
 * @author HeJiaLe
 * @date 2023/08/07 16:05
 */
    @ApiLog
    @Override
    public List<Node> doQueryResourceByRoleId(@NotNull @Valid
                                                  @RequestBody RoleQuery roleQuery) {

        return roleService.queryResourceByRoleId(roleQuery.getId());
    }

    @ApiLog
    @Override
    public List<Node> selectCompany() {
        return roleService.selectCompany();
    }

    @ApiLog
    @Override
    public boolean haveRole(@NotNull @Valid @RequestBody RoleDTO roleDTO) {
        return roleService.haveRole(roleDTO);
    }

    @ApiLog
    @Override
    public Boolean add(@NotNull @Valid @RequestBody RoleDTO roleDTO) {
        return roleService.add(roleDTO);
    }

    @ApiLog
    @Override
    public Boolean remove(@ApiParam(value = "当前行id", required = true) @NotNull @Valid Long id,
                          @ApiParam(value = "当前行版本", required = true)@NotNull @Valid Long version) {
        return roleService.remove(id, version);
    }

    @ApiLog
    @Override
    public Boolean batchRemove(@NotNull @Valid @RequestBody RoleDeleteDTO[] roleDeleteDTOS) {
        return roleService.batchRemove(roleDeleteDTOS);
    }

    @ApiLog
    @Override
    public Boolean modify(@NotNull @Valid @RequestBody RoleDTO roleDTO) {
        return roleService.modify(roleDTO);
    }

    @ApiLog
    @Override
    public RoleVO copy(@NotNull @Valid Long id) {
        return roleService.copy(id);
    }

    @ApiLog
    @Override
    public CommonPage<RoleVO> commonQuery(@NotNull @Valid RoleQuery roleQuery) {
        return  roleService.commonQuery(roleQuery);
    }

    @ApiLog
    @Override
    public CommonPage<UserDTO> commonQueryUser(@NotNull @Valid UserQuery userQuery) {
        return  roleService.commonQueryUser(userQuery);
    }

    @ApiLog
    @Override
    public RoleVO queryRoleById(@NotNull  @Valid Long id) {
        return roleService.queryById(id);
    }

    @Override
    public RoleVO queryRoleResouce(@NotNull  @Valid Long id) {
        return roleService.queryRoleResouce(id);

    }

    @Override
    public RoleCompanyVO queryRoleCompanyById(@NotNull @Valid Long id) {
        return roleService.queryRoleCompanyById(id);
    }

    @Override
    public Boolean modifyRoleCompany(@NotNull @Valid @RequestBody RoleCompanyDTO roleCompanyDTO) {
        return roleService.modifyRoleCompany(roleCompanyDTO);
    }
}
